package mmi.gui.shape;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.util.Vector;

/**
 * FreeLine is a collection of points.
 * 
 * @author louis
 *
 */
public class FreeLine extends Shape {

	Vector<Point2D> points = new Vector<Point2D>();
	
	public FreeLine(Point2D start, Point2D end) {
		super(start, end);
	
	}
	
	public FreeLine(Point2D start, Point2D end, double height) {
		super(start, end);
		
		points.add(new Point((int) start.getX(), (int) start.getY()));
	}
	
	public FreeLine(Point2D start, Point2D end, Color lineColour, int lineWidth) {
		super(start, end, lineColour, lineColour, lineWidth);
		
		points.add(new Point((int) start.getX(), (int) start.getY()));
	}
	
	public FreeLine(int id, Point2D start, Point2D end) {
		super(id, start, end);
		
		points.add(new Point((int) start.getX(), (int) start.getY()));
	}
	
	public FreeLine(int id, Point2D start, Point2D end, Color lineColour, int lineWidth) {
		super(id, start, end, lineColour, lineColour, lineWidth);
		
		points.add(new Point((int) start.getX(), (int) start.getY()));
	}

	@Override
	public boolean contains(Point2D position) 
	{
		for(Point2D p : points)
		{
			if(position.distance(p) <= lineWidth)
			{
				return true;
			}
		}

		return false;
	}

	/**
	 * Add a point to the collection of points, these are the points that are the essential part of the
	 * free line
	 * @param x
	 * @param y
	 */
	public void addPoint(Point2D point)
	{
		points.add(point);
	}
	
	/**
	 * Move the points a given distance
	 * @param xdif
	 * @param ydif
	 */
	public void movePoints(double xdif, double ydif)
	{
		for(Point2D p : points)
		{
			p.setLocation(p.getX() - xdif, p.getY() - ydif);
		}
	}
	
	/**
	 * @return the points
	 */
	public Vector<Point2D> getPoints() {
		return points;
	}

	/**
	 * @param points the points to set
	 */
	public void setPoints(Vector<Point2D> points) {
		this.points = points;
	}

	@Override
	public void draw(Graphics2D g) {
		Color defColour = g.getColor();
		if(lineColour == null)
			g.setColor(Color.white);
		else
			g.setColor(fillColour);
		
		for(Point2D p : points)
		{
                        g.fillOval((int) p.getX(), (int) p.getY(), lineWidth, lineWidth);
			//g.drawOval((int) p.getX(), (int) p.getY(), lineWidth, lineWidth);
		}
		g.setColor(defColour);
		
		drawBoundingBox(g);
	}

	@Override
	public void fill(Graphics2D g) {
		// TODO Auto-generated method stub

	}

	@Override
	public String getXMLRepresentation() {
		String s = "";
		
		s += "\t\t<Shape shapetype=\"FreeLine\" id=\"" + id + "\">" + "\n";
		s += "\t\t\t<xstart>" + start.getX() + "</xstart>" + "\n";
		s += "\t\t\t<ystart>" + start.getY() + "</ystart>" + "\n";
		s += "\t\t\t<xend>" + end.getX() + "</xend>" + "\n";
		s += "\t\t\t<yend>" + end.getY() + "</yend>" + "\n";
		s += "\t\t\t<linecolour>" + lineColour + "</linecolour>" + "\n";
		s += "\t\t\t<linewidth>" + lineWidth + "</linewidth>" + "\n";
		s += "\t\t</Shape>";
		
		for(Point2D p : points)
		{
			s += "\n\t\t<Shape shapetype=\"FreeLine\" id=\"" + id + "\">" + "\n";
			s += "\t\t\t<xstart>" + p.getX() + "</xstart>" + "\n";
			s += "\t\t\t<ystart>" + p.getY() + "</ystart>" + "\n";
			s += "\t\t\t<xend>" + end.getX() + "</xend>" + "\n";
			s += "\t\t\t<yend>" + end.getY() + "</yend>" + "\n";
			s += "\t\t\t<linecolour>" + lineColour + "</linecolour>" + "\n";
			s += "\t\t\t<linewidth>" + lineWidth + "</linewidth>" + "\n";
			s += "\t\t</Shape>";
		}

		return s;
	}
	
	@Override
	public String toString() {
		String s = "";
		
		s += "FreeLine [" + id + "]: " + start.getX() + " " + start.getY() + " " + end.getX() + " " + end.getY() + "\n";
		for(Point2D p : points)
		{
			s += p.getX() + ", " + p.getY() + "\n";
		}
		
		return s;
	}

	@Override
	public Shapes getShape() {
		return Shapes.FreeLine;
	}

	@Override
	public void makeTheShape()
	{

	}
}
